package org.syh.boyangfighthealthclub.service.impl;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.syh.boyangfighthealthclub.dao.IUserDao;
import org.syh.boyangfighthealthclub.dao.impl.UserDaoImpl;
import org.syh.boyangfighthealthclub.entity.User;
import org.syh.boyangfighthealthclub.service.IUserService;
import org.syh.boyangfighthealthclub.utils.JavaDateConnection;

import java.sql.Connection;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2021/3/9 9:37
 */
public class UserServiceImpl implements IUserService {
    IUserDao iUserDao=new UserDaoImpl();
    JavaDateConnection javaDateConnection=new JavaDateConnection();
    @Override
    public boolean insertUser(User user) {
      User returnUser=iUserDao.selectUserByNickName(javaDateConnection.getConn(),user.getNickname());
      if (returnUser==null){
        boolean judgment=iUserDao.insertUser(javaDateConnection.getConn(),user);
        if (judgment){
            System.out.println("用户添加成功");
            return true;
        }
      }
        System.out.println("用户添加失败");
        return false;
    }

    @Override
    public boolean deleteUser(int id) {
        return iUserDao.deleteUser(javaDateConnection.getConn(),id);
    }

    @Override
    public boolean updateUser(User user)
    {
        return iUserDao.updateUser(javaDateConnection.getConn(),user);
    }

    @Override
    public List<User> selectUsers(int pageNumber, int pageSize) {
        return  iUserDao.selectUsers(javaDateConnection.getConn(),pageNumber,pageSize);
    }
    @Override
    public boolean loginUser(String nickName, String password){
      User user =iUserDao.selectUserByNickName(javaDateConnection.getConn(),nickName);
        System.out.println(user);
      if (user.getNickname().equals(nickName)&&user.getPassword().equals(password)&& "用户".equals(user.getRole())){
          System.out.println("用户登录成功");
          return true;
      }
        System.out.println("用户登录失败");
        return false;
    }

    @Override
    public boolean loginAdmin(String nickName, String password) {
        User user =iUserDao.selectUserByNickName(javaDateConnection.getConn(),nickName);
        if (user.getNickname().equals(nickName)&&user.getPassword().equals(password)&& "管理员".equals(user.getRole())){
            System.out.println("管理员登录成功");
            return true;
        }
        System.out.println("管理员登录失败");
        return false;
    }

    @Override
    public int selectCountUsers() {
      return   iUserDao.selectCountUsers(javaDateConnection.getConn());
    }


    @Override
    public User selectUserById(Integer id) {
     return    iUserDao.selectUserById(javaDateConnection.getConn(),id);
    }
}
